<?php


namespace app\api\controller\v19;


use app\api\controller\v2\Auth;
use app\common\cachemodel\BirthBoon;
use app\common\cachemodel\ZlBirthdayWish;
use think\Cache;

/**
 * @version 2.2.0
 * Class Birthday
 * @package app\api\controller\v19
 */
class Birthday extends Auth
{
    /**
     * 领取生日权益
     */
    public function getBoon()
    {
        $uid = (int)$this->user['id'];
        $timezone = $this->headers['timezone'] ?? 8;

        $birthdayBoonModel = new BirthBoon();
        $giveResult = $birthdayBoonModel->birthdayCouponGive($uid);
        $date = gmdateModify('Ymd', time(), 8, $timezone);

        $giveflagCacheKey = 'birthday:give:flag:' . $date . ':' . $uid; // 生日权益领取标志  今天领取到权益标志为1；不弹领取失败错误提示

        if ($giveResult)
        {
            Cache::set($giveflagCacheKey, 1, 86400);
            return show(config('code.success'), '权益领取成功！', ['is_show_msg' => 1]); // 弹成功提示
        } else
        {
            $flag = Cache::get($giveflagCacheKey);
            if ($flag)
            {
                return show(config('code.success'), '权益领取成功！', ['is_show_msg' => 0]); // 不弹成功提示
            } else
            {
                return show(config('code.success'), $birthdayBoonModel->errorMsg, ['is_show_msg' => 1]); // 弹失败提示
            }
        }
    }

    /**
     * 发送祝福
     */
    public function sendWish()
    {
        $rid = input('rid/d', 0);
        $id = input('id/d', 0);
        $content = input('content/s', '');

        if (!$rid || empty($content) || mb_strlen($content) > 50)
        {
            return show(config('code.error'), '服务异常，请稍后再试！');
        }

        $data = [
            'id' => $id,
            'uid' => $this->user['id'],
            'self_rid' => $this->user['rid'],
            'send_rid' => $rid,
            'wish' => $content,
        ];

        $res = (new ZlBirthdayWish())->sendWish($data);

        if ($res['res'])
        {
            return show(config('code.success'), '祝福成功', ['id' => $res['id']]);
        } else
        {
            return show(config('code.error'), '服务异常，请稍后再试！');
        }
    }
}
